import http from '@/utils/http'
import type {
  PaginationParams,
  PaginationResponse,
  ServerInfo,
  RoomInfo,
  CabinetInfo,
  BMCInfo,
  NetworkCard,
  NetworkPort,
} from './types'

export const assetService = {
  // 获取服务器列表
  getServersByRoom: (roomId: number, params: PaginationParams) => {
    return http.get<PaginationResponse<ServerInfo>>(`/asset/servers/room/${roomId}`, {
      params: {
        page: params.page,
        page_size: params.pageSize,
      },
    })
  },

  // 获取单个服务器详情
  getServerById: (id: number) => {
    return http.get<never, ServerInfo>(`/asset/servers/${id}`)
  },

  // 创建服务器
  createServer: (data: Partial<ServerInfo>) => {
    return http.post<never, ServerInfo>('/asset/servers', data)
  },

  // 更新服务器信息
  updateServer: (data: Partial<ServerInfo> & { id: number }) => {
    return http.put<never, ServerInfo>('/asset/servers', data)
  },

  // 删除服务器
  deleteServer: (id: number) => {
    return http.delete<never, void>(`/asset/servers/${id}`)
  },

  // 获取机房列表
  getRooms: (params: PaginationParams) => {
    return http.get<never, PaginationResponse<RoomInfo>>('/rooms', {
      params: {
        page: params.page,
        page_size: params.pageSize,
      },
    })
  },

  // 获取机柜列表
  getCabinetsByRoomId: (roomId: number) => {
    return http.get<never, { id: number; name: string }[]>(`/asset/rooms/${roomId}/cabinets`)
  },

  // 获取机柜列表
  getCabinets: (roomId: number, params: PaginationParams) => {
    return http.get<never, PaginationResponse<CabinetInfo>>(`/cabinets/room/${roomId}`, {
      params: {
        page: params.page,
        page_size: params.pageSize,
      },
    })
  },

  // 创建机柜
  createCabinet: (data: Partial<CabinetInfo>) => {
    return http.post<never, CabinetInfo>('/cabinets', data)
  },

  // 更新机柜
  updateCabinet: (data: Partial<CabinetInfo>) => {
    return http.put<never, CabinetInfo>('/cabinets', data)
  },

  // 删除机柜
  deleteCabinet: (id: number) => {
    return http.delete<never, void>(`/cabinets/${id}`)
  },

  // 创建机房
  createRoom: (data: Partial<RoomInfo>) => {
    return http.post<never, RoomInfo>('/rooms', data)
  },

  // 更新机房
  updateRoom: (data: Partial<RoomInfo> & { room_id: number }) => {
    return http.put<never, RoomInfo>('/rooms', data)
  },

  // 删除机房
  async deleteRoom(roomId: number): Promise<void> {
    await http.delete(`/rooms/${roomId}`)
  },

  // 部署服务器
  deployServer: (id: number) => {
    return http.post<never, void>(`/asset/servers/${id}/deploy`)
  },

  // 获取服务器的BMC信息
  getBMCInfo: (serverId: number) => {
    return http.get<never, BMCInfo>(`/asset/servers/bmc/server/${serverId}`)
  },

  // 重置服务器配置（设置PXE启动）
  resetServerConfig: (serverId: number) => {
    return http.put<never, void>(`/asset/servers/${serverId}/reset`)
  },

  // 重启服务器
  rebootServer: (serverId: number) => {
    return http.post<never, void>(`/asset/servers/${serverId}/reboot`)
  },

  // 获取服务器的网卡列表
  getServerNetworkCards: (serverId: number) => {
    return http.get<never, NetworkCard[]>(`/asset/servers/networkcard/server/${serverId}`)
  },

  // 获取网卡的网口列表
  getNetworkPorts: (networkCardId: number) => {
    return http.get<never, NetworkPort[]>(`/asset/servers/networkport/card/${networkCardId}`)
  },

  // 创建网口
  createNetworkPort: (data: Partial<NetworkPort>) => {
    return http.post<never, NetworkPort>('/asset/servers/networkport', data)
  },

  // 创建网卡
  createNetworkCard: (data: Partial<NetworkCard>) => {
    return http.post<never, NetworkCard>('/asset/servers/networkcard', data)
  },

  // 删除网口
  deleteNetworkPort: (id: number) => {
    return http.delete<never, void>(`/asset/servers/networkport/${id}`)
  },

  // 更新网口
  updateNetworkPort: (data: Partial<NetworkPort> & { id: number }) => {
    return http.put<never, NetworkPort>('/asset/servers/networkport', data)
  },

  // 创建 BMC
  createBMC: (data: Partial<BMCInfo>) => {
    return http.post<never, BMCInfo>('/asset/servers/bmc', data)
  },
}
